<?php

class Address_AddressController extends Zend_Controller_Action
{

    public function init()
    {
        $contextSwitch = $this->_helper->getHelper('contextSwitch');
        $contextSwitch->addActionContext('list', 'json')
		      ->addActionContext('edit', 'json')
		      ->addActionContext('view', 'json')
                      ->initContext();
                    
    }

    public function indexAction()
    {
        // action body
    }

    public function listAction()
    {
//	$this->_helper->layout()->disableLayout();
//        $this->_helper->viewRenderer->setNoRender(true);
	$addressModel = new Address_Model_DbTable_Address();
	$items = $addressModel->fetchAll(null, 'name1');
	$this->view->identifier = 'address_id';
	$this->view->items = $items->toArray();
	
    }

    public function editAction()
    {
        $this->_helper->layout()->disableLayout();
	Zend_Dojo::enableView($this->view);
	Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
	
	$form = new Address_Form_Address();
	if($this->getRequest()->isPost() && $form->isValid($_POST))
	{
	    $data = $form->getValues();
	    unset($data['address_submit']);
	    print_r($data);
	    $addressModel = new Address_Model_DbTable_Address();
	    if(isset($data['address_id']) && is_numeric($data['address_id']))
	    {
		$addressModel->update($data, 'address_id = '.$data['address_id']);
	    }
	    else
	    {
		$addressModel->insert($data);
	    }
	}
	else
	{
	    $this->view->form = $form;
	}
	
    }

    public function viewAction()
    {
        $addressModel = new Address_Model_DbTable_Address();
	
	$item = $addressModel->fetchRow('address_id = '. $this->getRequest()->getParam('address_id'))->toArray();
	$this->view->item = $item;
    }	


}







